// This file contains material supporting section 10.9 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com

/*
 * SimpleClient.java   2001-02-08
 *
 * Copyright (c) 2001 Robert Laganiere and Timothy C. Lethbridge.
 * All Rights Reserved.
 *
 */
package control;

import java.awt.List;
import java.awt.Color;

import javax.swing.JTextArea;

import client.AbstractClient;
import client.Message;
import db.parking.beans.Admin;
import db.parking.beans.Complaints;
import db.parking.beans.Orders;
import db.tables.AdminOrders;
import javax.swing.JOptionPane;
import vcp.gui.*;

public class SimpleClient extends AbstractClient {
	private List liste;
	private JTextArea textArea;

	public SimpleClient(List liste) {
		super("localhost", 12345);
		this.liste = liste;
	}

	public SimpleClient(int port, List liste) {
		super("localhost", port);
		this.liste = liste;
	}

	public SimpleClient(String host, int port) {
		super(host, port);
	}

	/**
	 * Hook method called after the connection has been closed.
	 */
	protected void connectionClosed() {
		liste.add("**Connection closed**");
		textArea.setBackground(Color.red);
	}

	/**
	 * Hook method called each time an exception is thrown by the client's
	 * thread that is waiting for messages from the server.
	 * 
	 * @param exception
	 *            the exception raised.
	 */
	protected void connectionException(Exception exception) {
		textArea.append("**Connection exception: " + exception);
		textArea.setBackground(Color.red);
	}

	/**
	 * Hook method called after a connection has been established.
	 */
	protected void connectionEstablished() {
		System.out.println("Connected To Server Succesfully\n");
	}

	/**
	 * Handles a message sent from the server to this client.
	 * 
	 * @param msg
	 *            the message sent.
	 */
	protected void handleMessageFromServer(Object msg) {
		Message tempMsg = (Message) msg;
		switch (tempMsg.getMsg()) {
		case "AddOccasionalOrder":
			if (tempMsg.isStatus()) {
				JOptionPane.showMessageDialog(null, "Order is Succesful");
			} else {
				JOptionPane.showMessageDialog(null, "Order has Failed: "
						+ tempMsg.getStatusMsg(), "DATABASE ERROR",
						JOptionPane.ERROR_MESSAGE);
			}
			break;

		case "AddTemporaryOrder":
			if (tempMsg.isStatus()) {
				JOptionPane.showMessageDialog(null, "Order is Succesful");
			} else {
				JOptionPane.showMessageDialog(null, "Order has Failed: "
						+ tempMsg.getStatusMsg(), "DATABASE ERROR",
						JOptionPane.ERROR_MESSAGE);
			}
			break;

		case "AddSubscriberOrder":
			if (tempMsg.isStatus()) {
				JOptionPane.showMessageDialog(null, "Order is Succesful");
			} else {
				JOptionPane.showMessageDialog(null, "Order has Failed: "
						+ tempMsg.getStatusMsg(), "DATABASE ERROR",
						JOptionPane.ERROR_MESSAGE);
			}
			break;
			
		case "SubmitComplaint":
			if(tempMsg.isStatus())
			{
				JOptionPane.showMessageDialog(null , tempMsg.getStatusMsg());
			}	
			else
			{
				JOptionPane.showMessageDialog(null,"Complaint has Failed: " + tempMsg.getStatusMsg()+":" + ((Complaints)tempMsg.getObj()).getOrderID());
																																			
			}	
			
			break;
			
			
			
		}
	}
}